//
//  BookListViewController.m
//  FunRead
//
//  Created by  King on 15/5/22.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "BookListViewController.h"
#import "BookListModel.h"
#import "BookListTableViewCell.h"
#import "BookListDetailViewController.h"
@interface BookListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation BookListViewController
- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 初始化tableArray数组
        self.tableArray = [NSMutableArray array];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏和tabBar为不透明
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"排行榜";
    self.hidesBottomBarWhenPushed = YES;
    
    // 设置导航返回键图片
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbefore.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.leftBarButtonItem = button;
    [button release];
    
    [self addContent];
    // 加载tableView界面
    [self createTableView];
}
- (void)action:(id)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 添加给selfArray内容
- (void)addContent
{
    BookListModel *hotModel = [[BookListModel alloc] init];
    hotModel.categoryName = @"最热下载榜 TOP100";
    hotModel.categoryPicName = @"下载.png";
    
    BookListModel *readModel = [[BookListModel alloc] init];
    readModel.categoryName = @"读者留存榜 TOP100";
    readModel.categoryPicName = @"读者.png";
    
    BookListModel *classicModel = [[BookListModel alloc] init];
    classicModel.categoryName = @"经典完结榜";
    classicModel.categoryPicName = @"经典.png";
    
    BookListModel *weekModel = [[BookListModel alloc] init];
    weekModel.categoryName = @"本周潜力榜";
    weekModel.categoryPicName = @"本周.png";
    
    BookListModel *originModel = [[BookListModel alloc] init];
    originModel.categoryName = @"起点月票榜";
    originModel.categoryPicName = @"起点.png";
    
    BookListModel *freelyModel = [[BookListModel alloc] init];
    freelyModel.categoryName = @"纵横月票榜";
    freelyModel.categoryPicName = @"纵横.png";
    
    BookListModel *seventeenModel = [[BookListModel alloc] init];
    seventeenModel.categoryName = @"17K鲜花榜";
    seventeenModel.categoryPicName = @"17K";
    
    BookListModel *creationModel = [[BookListModel alloc] init];
    creationModel.categoryName = @"创世月票榜";
    creationModel.categoryPicName = @"创世.png";
    
    BookListModel *jinJiangModel = [[BookListModel alloc] init];
    jinJiangModel.categoryName = @"晋江月票榜";
    jinJiangModel.categoryPicName = @"晋江.png";
    
    BookListModel *xiaoXiangModel = [[BookListModel alloc] init];
    xiaoXiangModel.categoryName = @"潇湘月票榜";
    xiaoXiangModel.categoryPicName = @"潇湘.png";
    
    [self.tableArray addObject:hotModel];
    [self.tableArray addObject:readModel];
    [self.tableArray addObject:classicModel];
    [self.tableArray addObject:weekModel];
    [self.tableArray addObject:originModel];
    [self.tableArray addObject:freelyModel];
    [self.tableArray addObject:seventeenModel];
    [self.tableArray addObject:creationModel];
    [self.tableArray addObject:jinJiangModel];
    [self.tableArray addObject:xiaoXiangModel];
    
    [hotModel release];
    [readModel release];
    [classicModel release];
    [weekModel release];
    [originModel release];
    [freelyModel release];
    [seventeenModel release];
    [creationModel release];
    [jinJiangModel release];
    [xiaoXiangModel release];
}

// 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    // 设置代理
    _tableView.dataSource = self;
    _tableView.delegate = self;
    // 设置行高，背景颜色，分割线颜色
    _tableView.rowHeight = 50;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorColor = [UIColor grayColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    BookListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[BookListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    BookListModel *bookListModel = [_tableArray objectAtIndex:indexPath.row];

    cell.categoryImage.image = [UIImage imageNamed:bookListModel.categoryPicName];
    cell.categoryName.text = bookListModel.categoryName;
    cell.detailTextLabel.text = @"〉";
    return cell;
}
// 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookListDetailViewController *detail = [[BookListDetailViewController alloc] init];
    detail.array = self.tableArray;
    detail.categoryTag = indexPath.row;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}
@end
